package com.sanjiahantang.youjiarole.module.common.views.impl;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextPaint;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.sanjiahantang.plugin.swipemenulistview.SwipeMenuCreator;
import com.sanjiahantang.plugin.swipemenulistview.SwipeMenuListView;
import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.base.BaseMvpFragment;
import com.sanjiahantang.youjiarole.module.common.presenters.ShoppingCartPresenter;
import com.sanjiahantang.youjiarole.module.common.views.ShoppingCartView;


public class ShoppingCartFragment extends BaseMvpFragment<ShoppingCartView,ShoppingCartPresenter>
        implements ShoppingCartView,CompoundButton.OnCheckedChangeListener,View.OnClickListener {

    private LinearLayout pullRefreshLayout;
    private LinearLayout pullRefreshLayout2;



    //购物车列表
    private SwipeMenuListView mListView;
    //已登录并且购物车中有数据
    private FrameLayout mFrameLayoutLogin;
    //购物车为空，提示登录窗口
    private TableRow mTableRowToLogin;


    private Button mButtonToLogin;
    private View mButtonFocus,vsNetworkError;
    private Button mButtonSettleAccounts;
    private Button mButtonMoveFocus;
    private Button mButtonDelete;

    private TableRow mTableRowGoodsSettle;
    private TableRow mTableRowGoodsDelete;

    //去结算
    private TextView mTextViewTotalPrice;
    private TextView mTextViewTotalAccounts;

    private CheckBox mCheckBoxSelectAll;
    private CheckBox mCheckBoxDeleteAll;


    private View view;




    //网络错误用到的view





    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (view != null) {
            ViewGroup parent = (ViewGroup) view.getParent();
            if (parent != null) {
                parent.removeView(view);
            }
        } else {
            view = inflater.inflate(R.layout.fragment_message, container, false);
        }
        return view;
    }

    @Override
    protected void initView() {

        initTitle();
        initPullRefreshLayout();
        initPullRefreshLayout2();


        mFrameLayoutLogin = (FrameLayout) view.findViewById(R.id.loginLayout);
        mListView = (SwipeMenuListView) view.findViewById(R.id.list);
        mTableRowToLogin = (TableRow) view.findViewById(R.id.login);
        mTableRowGoodsSettle = (TableRow) view.findViewById(R.id.goods_settle);
        mTableRowGoodsDelete = (TableRow) view.findViewById(R.id.goods_delete);
        mButtonToLogin = (Button) view.findViewById(R.id.toLogin);
        mButtonFocus = view.findViewById(R.id.focus);
        mButtonSettleAccounts = (Button) view.findViewById(R.id.settle_accounts);
        mButtonMoveFocus = (Button) view.findViewById(R.id.movefocus);
        mButtonMoveFocus.setVisibility(View.INVISIBLE);
        mButtonDelete = (Button) view.findViewById(R.id.delete);
        mTextViewTotalPrice = (TextView) view.findViewById(R.id.total_sum);
        mTextViewTotalAccounts = (TextView) view.findViewById(R.id.total_accounts);
        mCheckBoxSelectAll = (CheckBox) view.findViewById(R.id.select_all);
        mCheckBoxDeleteAll = (CheckBox) view.findViewById(R.id.select_all_delete);
        vsNetworkError =  view.findViewById(R.id.vsNetworkError);

        mCheckBoxSelectAll.setChecked(false);
        mButtonToLogin.setOnClickListener(this);
        mButtonFocus.setOnClickListener(this);
        mButtonSettleAccounts.setOnClickListener(this);
        mButtonDelete.setOnClickListener(this);
        mCheckBoxSelectAll.setOnCheckedChangeListener(this);
        mCheckBoxDeleteAll.setOnCheckedChangeListener(this);

        presenter.init();
    }


    //商品列表下拉刷新
    private void initPullRefreshLayout() {
        pullRefreshLayout = (LinearLayout) getView().findViewById(R.id.pullRefreshLayout);
//        pullRefreshLayout.setRefreshStyle(PullRefreshLayout.STYLE_MATERIAL);
//        pullRefreshLayout.setOnRefreshListener(new PullRefreshLayout.OnRefreshListener() {
//            @Override
//            public void onRefresh() {
//                pullRefreshLayout.postDelayed(new Runnable() {
//                    @Override
//                    public void run() {
//                        presenter.reloadData(context);
//                        pullRefreshLayout.setRefreshing(false);
//                    }
//                }, 1000);
//            }
//
//            @Override
//            public void onMove(boolean ismove) {
//
//            }
//        });
    }

    //商品列表为空，下拉刷新
    private void initPullRefreshLayout2() {
        pullRefreshLayout2 = (LinearLayout) getView().findViewById(R.id.pullRefreshLayout2);
//        pullRefreshLayout2.setRefreshStyle(PullRefreshLayout.STYLE_MATERIAL);
//        pullRefreshLayout2.setOnRefreshListener(new PullRefreshLayout.OnRefreshListener() {
//            @Override
//            public void onRefresh() {
//                pullRefreshLayout2.postDelayed(new Runnable() {
//                    @Override
//                    public void run() {
//                        presenter.reloadData(context);
//                        pullRefreshLayout2.setRefreshing(false);
//                    }
//                }, 1000);
//            }
//
//            @Override
//            public void onMove(boolean ismove) {
//
//            }
//        });
    }

    @Override
    protected void initData() {
        mListView.setAdapter(presenter.getAdapter(context));
    }



    private void initTitle() {

        Log.d("order", "initTitle");
        title = (TextView) getView().findViewById(R.id.title);
        title.setText(R.string.main_cart);
        TextPaint paint=title.getPaint();
//        paint.setFakeBoldText(true);
        if (this.getTag().equals("cartFragment")) {
            back.setVisibility(View.VISIBLE);
            back.setOnClickListener(this);
        }
    }

    @Override
    public ShoppingCartPresenter initPresenter() {
        return new ShoppingCartPresenter();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (Utils.NO_NETWORK_STATE == Utils.isNetworkAvailable(context)) {
            onNetError(true);
            return;
        }else{
            onNetError(false);
        }
        presenter.reloadData(context);
    }

    private void onNetError(boolean b) {
        if (b){
            vsNetworkError.setVisibility(View.VISIBLE);
            vsNetworkError.findViewById(R.id.btnReloadNetwork).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onResume();
                }
            });
        }else{
            vsNetworkError.setVisibility(View.GONE);
        }
    }

    @Override
    public void switchLayout() {
        if (pullRefreshLayout != null)
        pullRefreshLayout.setVisibility(View.GONE);
        if (pullRefreshLayout2 != null)
        pullRefreshLayout2.setVisibility(View.GONE);
    }

    @Override
    public void hideNetError() {
    }

    @Override
    public void initMListView(SwipeMenuCreator creator, SwipeMenuListView.OnMenuItemClickListener onMenuItemClickListener) {
        mListView.setMenuCreator(creator);
        mListView.setOnMenuItemClickListener(onMenuItemClickListener);
    }

    @Override
    public void checkButton(@Nullable Boolean selectAll,@Nullable Boolean deleteAll) {
        if(selectAll != null){
            mCheckBoxSelectAll.setChecked(selectAll);
        }
        if(deleteAll != null){
            mCheckBoxDeleteAll.setChecked(deleteAll);
        }

    }

    @Override
    public void setPrice(String totalPrice, int totalNumber) {
        if (totalPrice.equals("0")){
            mButtonSettleAccounts.setText("结算(0)");
        }else {
            mButtonSettleAccounts.setText(getResources().getString(R.string.cart_goods_settle_accounts));
        }
        mTextViewTotalPrice.setText(String.format(getResources().getString(R.string.cart_goods_total_sum), totalNumber));
        mTextViewTotalAccounts.setText(String.format(getResources().getString(R.string.cart_goods_total_accounts), totalPrice));

    }

    @Override
    public void hideCart() {
        mFrameLayoutLogin.setVisibility(View.VISIBLE);
        pullRefreshLayout2.setVisibility(View.GONE);
        mTableRowToLogin.setVisibility(View.GONE);
    }

    @Override
    public void showCart() {
        mFrameLayoutLogin.setVisibility(View.VISIBLE);
        pullRefreshLayout2.setVisibility(View.GONE);
    }

    @Override
    public void showEmptyCart() {
        mFrameLayoutLogin.setVisibility(View.GONE);
        pullRefreshLayout2.setVisibility(View.VISIBLE);
        mTableRowToLogin.setVisibility(View.GONE);
    }

    @Override
    public void showToLoginLayout() {
        mFrameLayoutLogin.setVisibility(View.GONE);
        pullRefreshLayout2.setVisibility(View.VISIBLE);
        mTableRowToLogin.setVisibility(View.VISIBLE);
    }

    @Override
    public void onClick(View v) {

        if (Utils.NO_NETWORK_STATE == Utils.isNetworkAvailable(context)) {
            switchLayout();
            return;
        }
        switch (v.getId()) {
            case R.id.toLogin:
                presenter.toLogin(context);
                break;
            case R.id.focus:

                Intent intent = new Intent(context,ShopActivity.class);
                intent.putExtra(KEY_KEYWORDS, "");
                startActivity(intent);
                break;
            //去结算
            case R.id.settle_accounts:
                presenter.goPageCreateOrder(context);
                break;
            //编辑购物车中商品
            case R.id.right:

                if (ensure.getText().equals("编辑")) {
                    ensure.setText("完成");
                    mTableRowGoodsSettle.setVisibility(View.GONE);
                    mTableRowGoodsDelete.setVisibility(View.VISIBLE);
                } else if (ensure.getText().equals("完成")) {
                    ensure.setText("编辑");
                    mTableRowGoodsSettle.setVisibility(View.VISIBLE);
                    mTableRowGoodsDelete.setVisibility(View.GONE);
                }

                break;
            //移入关注
            case R.id.movefocus:
                Utils.showToast(getActivity(), "移入关注");
                break;
            case R.id.btnReloadNetwork:
                onResume();
                break;
            case R.id.back:
                getActivity().finish();
                break;
        }


    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        presenter.onCheckedChanged(buttonView, isChecked);
    }



    @Override
    public void showProgress() {
        context.showProgress(context);
    }

    @Override
    public void hideProgress() {
        context.hideProgress();
    }
}
